<?php
namespace app\admin\model\finance;
use think\Model;


class LargeWithdraw extends Model
{

    protected $name='large_withdraw';

    public function wxapp(){
        return $this->belongsTo("app\admin\model\wxapp\Wxapp");
    }






    /**
     * 订单列表
     * @param string $dataType
     * @param array $query
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($query = [])
    {
        // 检索查询条件
        !empty($query) && $this->setQueryWhere($query);
        // 获取数据列表
        return $this->with(['wxapp'])
            ->alias('large')
            ->field('large.*')
            ->join('wxapp', 'wxapp.wxapp_id = large.wxapp_id')
            ->where('large.is_delete', '=', 0)
            ->order(['large.apply_time' => 'desc','large.id' => 'desc'])
            ->paginate(10, false, [
                'query' => \request()->request()
            ]);
    }


    /**
     * 设置默认的检索数据
     * @param $query
     * @param array $default
     * @return array
     */
    protected function setQueryDefaultValue(&$query, $default = [])
    {
        $data = array_merge($default, $query);
        foreach ($query as $key => $val) {
            if (empty($val) && isset($default[$key])) {
                $data[$key] = $default[$key];
            }
        }
        return $data;
    }

    private function setQueryWhere($query)
    {
        // 设置默认的检索数据
        $params = $this->setQueryDefaultValue($query, [
            'apply_status' => '-1',
        ]);
        // 用户昵称/订单号
        if (isset($query['search']) && !empty($query['search'])) {
            $this->where('wxapp.wxapp_name', 'like', '%' . trim($query['search']) . '%');
        }
        // 分账状态
        $params['apply_status'] > -1 && $this->where('large.apply_status', '=', (int)$params['apply_status']);
        // 起始时间
        !empty($params['start_time']) && $this->where('large.apply_time', '>=', strtotime($params['start_time']));
        // 截止时间
        !empty($params['end_time']) && $this->where('large.apply_time', '<', strtotime($params['end_time']) + 86400);
    }

}